package com.lantosec.hmrp.modules.eval.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.modules.eval.entity.EvalItem;
import com.lantosec.hmrp.modules.eval.entity.EvalType;
import com.lantosec.hmrp.modules.eval.mapper.EvalTypeMapper;
import com.lantosec.hmrp.modules.eval.service.IEvalItemService;
import com.lantosec.hmrp.modules.eval.service.IEvalTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.eval.vo.EvalTypeVO;
import com.lantosec.hmrp.modules.system.entity.SysDictionaries;
import com.lantosec.hmrp.modules.system.entity.SysUser;
import com.lantosec.hmrp.modules.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

import static com.lantosec.hmrp.core.util.BeanUtils.entity2VO;

/**
 * <p>
 * 考评项目分类定义 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-13
 */
@Service
@Primary
@Log4j2
public class EvalTypeServiceImpl extends ServiceImpl<EvalTypeMapper, EvalType> implements IEvalTypeService {

    @Autowired
    private ISysUserService iSysUserService;


    @Override
    public PageVO<EvalType> page(PageVO<EvalType> page, String evalTypeName) {
        if(StringUtils.isNotEmpty(evalTypeName)){
            return this.page(page,new QueryWrapper<EvalType>().like(EvalType.ColName.eval_type_name.toString(),evalTypeName)
                                                                            .orderByAsc(EvalType.ColName.eval_type_order.toString()));
        }else {
            return this.page(page,new QueryWrapper<EvalType>().orderByAsc(EvalType.ColName.eval_type_order.toString()));
        }

    }

    @Override
    public void updateOrder(String evalTypeId, int evalTypeOrder) {
        EvalType evalType = new EvalType();
        evalType.setEvalTypeOrder(evalTypeOrder);
        this.update(evalType,new QueryWrapper<EvalType>().eq(EvalType.ColName.eval_type_id.toString(),evalTypeId));
    }

    @Override
    public String queryMaxId(String parentId) {
        String evalTypeId = null;
        if(StringUtils.isNotEmpty(parentId)){
            evalTypeId = baseMapper.queryMaxIdByParentId(parentId);
            return StringUtils.isEmpty(evalTypeId) ? parentId + "001" : evalTypeId;
        }else{
            evalTypeId = baseMapper.queryMaxId();
            return StringUtils.isEmpty(evalTypeId)? "001" : evalTypeId;
        }
    }

    @Override
    public String saveEvalType(EvalType entity) {
        //查询表中最大的id
        String evalTypeId = queryMaxId(entity.getParentId());
        entity.setEvalTypeId(evalTypeId);
        //通过管理人id查询管理人姓名
        String managerId = entity.getManagerId();
        SysUser sysUser = iSysUserService.getById(managerId);
        entity.setManagerName(sysUser.getName());
        if(StringUtils.isEmpty(entity.getParentId())){
            entity.setLeaf(0);
        }else {
            entity.setLeaf(1);
        }
        save(entity);
        return entity.getEvalTypeId();
    }

    @Override
    public void updateEvalType(EvalType entity) {
        //通过管理人id查询管理人姓名
        String managerId = entity.getManagerId();
        SysUser sysUser = iSysUserService.getById(managerId);
        entity.setManagerName(sysUser.getName());
        this.updateById(entity);
    }

    @Override
    public List<EvalTypeVO> getAll(String evalTypeName) {
        List<EvalType> evalTypeList = this.list(new QueryWrapper<EvalType>().like(evalTypeName != null,EvalType.ColName.eval_type_name.toString(),evalTypeName)
                                                                            .orderByAsc(EvalType.ColName.eval_type_order.toString()));
        List<EvalTypeVO> evalTypeVOList = entity2VO(evalTypeList);
        //查询项目分类下的项目
        evalTypeVOList.forEach(parm->{
            String evalTypeId = parm.getEvalTypeId();
            List<EvalItem> evalItemList = baseMapper.getEvalItemList(evalTypeId);
            parm.setEvalItemList(evalItemList);
        });
        return evalTypeVOList;
    }

    @Override
    public List<TreeNodeVo> getTree(String id){
        List<EvalType> list = new ArrayList<>();
        if(StringUtils.isNotEmpty(id)){
            //查出所有的字典树
            list = baseMapper.selectList(new QueryWrapper<EvalType>().eq(EvalType.ColName.parent_id.toString(),id));

        }else{

            list = baseMapper.selectList(new QueryWrapper<EvalType>().isNull(EvalType.ColName.parent_id.toString()));
        }
        List<TreeNodeVo> nodes = new ArrayList<TreeNodeVo>();
        //判断父类为空的
        for(int i=0;i<list.size();i++){
            String parentId = list.get(i).getEvalTypeId();
            if(null != parentId){
                //用父类节点为空的id赋值给父id去查子节点
                List<TreeNodeVo> list1 = getTree(parentId);
                nodes.add(new TreeNodeVo(list.get(i).getEvalTypeId(),list.get(i).getEvalTypeName(),list1));
            }
        }
        return nodes;
    }

    @Override
    public List<EvalTypeVO> getByEvalTarget(Integer evalTarget) {
        List<EvalType> evalTypeList = this.list(new QueryWrapper<EvalType>().orderByAsc(EvalType.ColName.eval_type_order.toString()));
        List<EvalTypeVO> evalTypeVOList = entity2VO(evalTypeList);
        //查询项目分类下的项目
        evalTypeVOList.forEach(parm->{
            String evalTypeId = parm.getEvalTypeId();
            List<EvalItem> evalItemList = baseMapper.getEvalItems(evalTypeId,evalTarget);
            parm.setEvalItemList(evalItemList);
        });
        return evalTypeVOList;
    }

    @Override
    public List<EvalTypeVO> getByEvalTargetApply(Integer evalTarget) {
        List<EvalType> evalTypeList = this.list(new QueryWrapper<EvalType>().orderByAsc(EvalType.ColName.eval_type_order.toString()));
        List<EvalTypeVO> evalTypeVOList = entity2VO(evalTypeList);
        //查询项目分类下的项目
        evalTypeVOList.forEach(parm->{
            String evalTypeId = parm.getEvalTypeId();
            List<EvalItem> evalItemList = baseMapper.getEvalItemsApply(evalTypeId,evalTarget);
            parm.setEvalItemList(evalItemList);
        });
        return evalTypeVOList;
    }
}
